package com.xxh.dataaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxh.dataaccess.dao.UserCollectionDao;
import com.xxh.dataaccess.dao.UserDao;
import com.xxh.dataaccess.dao.UserviewHistoryDao;
import com.xxh.dataaccess.dto.VideoDto;
import com.xxh.dataaccess.entity.*;
import com.xxh.dataaccess.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    UserDao userDao;

    @Autowired
    MongoTemplate mongoTemplate;


    @Autowired
    UserCollectionDao userCollectionDao;


    @Resource
    UserviewHistoryDao userviewHistoryDao;


    @Override
    public User findByUserName(String username) {

        return userDao.findAllByUsername(username);
    }

    @Override
    public IPage<Video> getCollectionByUid(Integer uid, Integer cur, Integer size) {
        Page<UserCollection> page = new Page<>(cur, size);
        //mysql 取数据
        IPage<UserCollection> collectionPage = userCollectionDao.selectPage(page, new QueryWrapper<UserCollection>().eq("uid", uid));
        List<UserCollection> list = collectionPage.getRecords();
        List<String> videoIdList = list.stream().filter(item -> {
            return item.getBv() != null;
        }).map(item -> {
            return item.getBv();
        }).collect(Collectors.toList());

        //mongo 里面取数据
        Query query = new Query();
        query.addCriteria(Criteria.where("bv").in(videoIdList));
        List<Video> videos = mongoTemplate.find(query, Video.class);
        IPage<Video> result = new Page<>(cur, size);
        result.setRecords(videos);
        result.setTotal(collectionPage.getTotal());
        return result;


    }

    @Override
    public IPage<Movie> getMovieCollectionByUid(Integer uid, Integer cur, Integer size) {
        Page<UserCollection> page = new Page<>(cur, size);

        //mysql 取数据
        IPage<UserCollection> collectionPage = userCollectionDao.selectPage(page, new QueryWrapper<UserCollection>().eq("uid", uid));
        List<UserCollection> list = collectionPage.getRecords();
        List<Integer> MovieIdList = list.stream().filter(item -> {
            return item.getMid() != null;
        }).map(item -> {
            return item.getMid();
        }).collect(Collectors.toList());

        //mongo 里面取数据
        Query query = new Query();
        query.addCriteria(Criteria.where("mid").in(MovieIdList));
        List<Movie> movies = mongoTemplate.find(query, Movie.class);
        IPage<Movie> result = new Page<>(cur, size);
        result.setRecords(movies);
        result.setTotal(collectionPage.getTotal());
        return result;

    }

    @Override
    public IPage<VideoDto> gethistoryByUid(Integer uid, Integer cur, Integer size) {
        Page<UserviewHistory> page = new Page<>(cur, size);

        //mysql 取数据
        IPage<UserviewHistory> collectionPage = userviewHistoryDao.selectPage(page, new QueryWrapper<UserviewHistory>().eq("uid", uid).orderByDesc("view_time"));
        List<UserviewHistory> list = collectionPage.getRecords();


        HashMap<String, Date> viewHistoryMap = new HashMap<>();
        List<String> videoIdList = list.stream().map(item -> {
            viewHistoryMap.put(item.getBv(), item.getViewTime());
            return item.getBv();
        }).collect(Collectors.toList());


        //mongo 里面取数据
        Query query = new Query();
        query.addCriteria(Criteria.where("bv").in(videoIdList));
        List<VideoDto> videos = mongoTemplate.find(query, VideoDto.class);
        IPage<VideoDto> result = new Page<>(cur, size);

        //设置浏览时间
        videos.forEach(v -> {
            Date viewdate = viewHistoryMap.get(v.getBv());
            v.setTime(viewdate);
        });
        result.setRecords(videos);
        result.setTotal(collectionPage.getTotal());
        return result;
    }

    @Override
    public String insert(User user) {
        User one = baseMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if(null!=one){
            return "用户名已存在";
        }else {
            baseMapper.insert(user);
            return "注册完成";
        }

    }
}
